//
//  HWHomeFunctionCell.m
//  HuaWan
//
//  Created by HuaWan Team on 2023.
//  Copyright © 2023 HuaWan Team. All rights reserved.
//

#import "HWHomeFunctionCell.h"

@implementation HWHomeFunctionCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    [_myButtons enumerateObjectsUsingBlock:^(UIButton *button, NSUInteger idx, BOOL * _Nonnull stop) {
        button.adjustsImageWhenHighlighted = NO;
    }];
    _myImageView.image = [UIImage imageNamed:@"home_bg"];
    
    _scrollView.delegate = self;
    [self setupSubViews];
    
    self->_timer = [NSTimer scheduledTimerWithTimeInterval:2.0
      target:self
    selector:@selector(daoJiShi)
    userInfo:nil
     repeats:YES];
}

- (void)setData:(id)data delegate:(id)delegate {
    _delegate = delegate;
}

- (IBAction)buttonAction:(UIButton *)sender {
    if (_delegate && [_delegate respondsToSelector:@selector(homeFunctionCell:button:)]) {
        [_delegate homeFunctionCell:self button:sender];
    }
}

- (void)dealloc {
    [_timer invalidate];
    _timer = nil;
}

- (NSArray *)dataAry {
    NSArray *ary = @[@{@"img":@"banner_01"},
                     
                     @{@"img":@"banner_02"},
                     
                     @{@"img":@"banner_03"},
    
    ];
    return ary;
}

- (void)setupSubViews
{
    // 移除之前的子控件
    [self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    // 移除之前的子控件
    [self.pageView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    __block float orginX = 0;
    [self.dataAry enumerateObjectsUsingBlock:^(NSDictionary *dic, NSUInteger idx, BOOL * _Nonnull stop) {
        NSString *ico = dic[@"img"];
        
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(idx * kScreenWidth, 0, kScreenWidth,kScreenWidth*400/375)];
        view.backgroundColor = [UIColor clearColor];
                
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(57,(view.frame.size.height - (view.frame.size.width - 115)*280/210) / 2, view.frame.size.width - 115, (view.frame.size.width - 115)*280/210)];
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.clipsToBounds = true;
        imageView.image = [UIImage imageNamed:ico];
        [view addSubview:imageView];
        
        [self.scrollView addSubview:view];
        
        UILabel *pageLabel = [[UILabel alloc] initWithFrame:CGRectMake(orginX, 0, 40, 4)];
        if (_index == idx) {
            pageLabel.backgroundColor = UIColorFromHex(0x000000);
        } else {
            pageLabel.backgroundColor = UIColorFromHexA(0x070804,0.15);
        }
        [_pageView addSubview:pageLabel];
        
        orginX += pageLabel.frame.size.width;
    }];
    self.scrollView.contentSize = CGSizeMake(kScreenWidth * self.dataAry.count, 0);
}

#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
//    NSLog(@"%f",scrollView.contentOffset.x);
//    _index = scrollView.contentOffset.x / kScreenWidth;
//
//    [self setupSubViews];
}

- (void)daoJiShi {
    if (_index >= 2) {
        _index = 0;
        [self.scrollView setContentOffset:CGPointMake(kScreenWidth * _index,               self.scrollView.contentOffset.y) animated:YES];
    } else {
        _index ++;
        [self.scrollView setContentOffset:CGPointMake(kScreenWidth * _index,               self.scrollView.contentOffset.y) animated:YES];
    }
    
    [self setupSubViews];
}

@end
